package com.example.monitorsms.http;


import android.os.Environment;

import com.example.monitorsms.MyApp;
import com.example.monitorsms.utils.LogUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.cookie.CookieJarImpl;
import com.lzy.okgo.cookie.store.MemoryCookieStore;
import com.lzy.okgo.request.GetRequest;
import com.lzy.okgo.request.PostRequest;

import java.io.File;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.OkHttpClient;
import okhttp3.Request;

/**
 * Created by cxf on 2018/9/17.
 */

public class HttpClient {
    public static final int TIMEOUT = 90000;
    private static HttpClient sInstance;
    private OkHttpClient mOkHttpClient;
    private String mLanguage;//语言
    private String mUrl;

    private HttpClient() {
        mUrl = "https://warn.qiaozui.top/index.php/api/";
    }

    public static HttpClient getInstance() {
        if (sInstance == null) {
            synchronized (HttpClient.class) {
                if (sInstance == null) {
                    sInstance = new HttpClient();
                }
            }
        }
        return sInstance;
    }

    public void init() {
        // 缓存目录
        File file = new File(Environment.getExternalStorageDirectory(), "a_cache");
        // 缓存大小
        int cacheSize = 10 * 1024 * 1024;
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(TIMEOUT, TimeUnit.MILLISECONDS);
        builder.readTimeout(TIMEOUT, TimeUnit.MILLISECONDS);
        builder.writeTimeout(TIMEOUT, TimeUnit.MILLISECONDS);
        builder.cookieJar(new CookieJarImpl(new MemoryCookieStore()));
        builder.retryOnConnectionFailure(true);
        builder.cache(new Cache(file, cacheSize)); // 配置缓存
        //输出HTTP请求 响应信息
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor("https");
        loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY);
        // loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.NONE);
        builder.addInterceptor(loggingInterceptor);
        builder.addInterceptor(chain -> {
            Request request = chain.request()
                    .newBuilder()
                    .addHeader("Connection", "keep-alive")
                    .build();
            return chain.proceed(request);
        });
        mOkHttpClient = builder.build();

        OkGo.getInstance().init(MyApp.getContext())
                .setOkHttpClient(mOkHttpClient)
                .setCacheMode(CacheMode.REQUEST_FAILED_READ_CACHE)
                .setRetryCount(0);
    }

    public GetRequest<JsonBeanData> getData(String serviceName, String tag) {
        return OkGo.<JsonBeanData>get(mUrl + serviceName)
                .tag(tag)
                .cacheKey(tag)
                .params(CommonHttpConsts.LANGUAGE, mLanguage);
    }

    public GetRequest<JsonBeanData> get(String serviceName, String tag) {
        return OkGo.<JsonBeanData>get(serviceName)
                .tag(tag)
                .cacheKey(tag)
                .params(CommonHttpConsts.LANGUAGE, mLanguage);
    }

    public PostRequest<JsonBeanData> postData(String serviceName, String tag) {
        LogUtils.e(mUrl + serviceName);
        return OkGo.<JsonBeanData>post(mUrl + serviceName)
                .tag(tag)
                .params(CommonHttpConsts.LANGUAGE, mLanguage);
    }

    public PostRequest<JsonBeanData> post(String serviceName, String tag) {
        LogUtils.e(serviceName);
        return OkGo.<JsonBeanData>post(serviceName)
                .tag(tag)
                .params(CommonHttpConsts.LANGUAGE, mLanguage);
    }

    public PostRequest<JsonBeanLoa> postLoa(String serviceName, String tag) {
        return OkGo.<JsonBeanLoa>post(serviceName)
                .tag(tag)
                .params(CommonHttpConsts.LANGUAGE, mLanguage);
    }

    public void cancel(String tag) {
        OkGo.cancelTag(mOkHttpClient, tag);
    }

    public void setLanguage(String language) {
        mLanguage = language;
    }
}
